//
//  YBMineOrderCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/7/1.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class YBMineOrderCell: UITableViewCell {

    private var goodsId: String = ""
    var onPay: ((_ goodsId: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        let line = UIView(color: kRGBColor(191, 190, 190))
        
        contentView.addSubview(_contentView)
        _contentView.addSubviews([productNameLabel, productSpecLabel, line, amountPayLabel, actualAmountPayLabel, orderTypeLabel, orderStatusLabel, payTimeLabel, overdueTimeLabel, goPayBtn])
        
        _contentView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(10)
        }
        
        productNameLabel.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(18)
        }
        
        productSpecLabel.snp.makeConstraints { make in
            make.top.equalTo(productNameLabel.snp.bottom).offset(8)
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(18)
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(0.3)
            make.top.equalTo(productSpecLabel.snp.bottom).offset(7)
        }
        
        amountPayLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(line.snp.bottom).offset(7)
            make.height.equalTo(18)
        }
        
        actualAmountPayLabel.snp.makeConstraints { make in
            make.centerY.height.equalTo(amountPayLabel)
            make.right.equalTo(-20)
        }
        
        orderTypeLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.height.equalTo(18)
            make.top.equalTo(amountPayLabel.snp.bottom).offset(8)
        }
        
        orderStatusLabel.snp.makeConstraints { make in
            make.centerY.height.equalTo(orderTypeLabel)
            make.right.equalTo(-20)
        }
        
        payTimeLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(18)
            make.top.equalTo(orderTypeLabel.snp.bottom).offset(8)
        }
        
        overdueTimeLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(18)
            make.top.equalTo(payTimeLabel.snp.bottom).offset(8)
            make.bottom.equalTo(-10)
        }
        
        goPayBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(100, 30))
            make.right.equalTo(-20)
            make.top.equalTo(orderStatusLabel.snp.bottom).offset(10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var _contentView: UIView = {
        let view = UIView(color: .lightWhiteDark33)
        view.bk_addStyleWith(cornerRadius: 4, corners: .allCorners)
        return view
    }()
    
    /// 产品名称
    private lazy var productNameLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    /// 产品规格
    private lazy var productSpecLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    /// 应付金额
    private lazy var amountPayLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    /// 实付金额
    private lazy var actualAmountPayLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, align: .right)
    /// 订单类型
    private lazy var orderTypeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    /// 订单状态
    private lazy var orderStatusLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, align: .right)
    /// 支付时间
    private lazy var payTimeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    /// 到期时间
    private lazy var overdueTimeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    
    private lazy var goPayBtn: UIButton = {
        let btn = self.bk_addGradientButton(title: "去支付", font: .systemFont(ofSize: 14, weight: .medium), titleColor: .white, gradientSize: CGSizeMake(100, 30), colors: [kRGBColor(88, 182, 255), kRGBColor(40, 128, 237)])
        btn.bk_addStyleWith(cornerRadius: 15, corners: .allCorners)
        btn.bk_addTarget { [unowned self] sender in
            self.onPay?(goodsId)
        }
        return btn
    }()
    
}

// MARK: - Public
extension YBMineOrderCell {
    
    func updateCell(by model: YBOrderModel) {
        goodsId = model.goodsId
        productNameLabel.attributedText = NSMutableAttributedString("产品名称：") { $0
            .foregroundColor(ColorDef.gray184)
        }.addText(model.productName) { $0
            .foregroundColor(.lightBlack51DarkLight230)
        }
        
        productSpecLabel.attributedText = NSMutableAttributedString("产品规格：") { $0
            .foregroundColor(ColorDef.gray184)
        }.addText(model.specs) { $0
            .foregroundColor(.lightBlack51DarkLight230)
        }
        
        amountPayLabel.attributedText = NSMutableAttributedString("应付金额：") { $0
            .foregroundColor(ColorDef.gray184)
        }.addText(model.orderAmount) { $0
            .foregroundColor(.red)
        }
        
        actualAmountPayLabel.attributedText = NSMutableAttributedString("实付金额：") { $0
            .foregroundColor(ColorDef.gray184)
        }.addText(model.payAmount) { $0
            .foregroundColor(.red)
        }
        
        orderTypeLabel.attributedText = NSMutableAttributedString("订单类型：") { $0
            .foregroundColor(ColorDef.gray184)
        }.addText(model.orderType) { $0
            .foregroundColor(.lightBlack51DarkLight230)
        }
        
        var status = "已取消"
        if model.status == 1 {
            status = "等待付款"
        } else if model.status == 2 {
            status = "已完成"
        }
        
        orderStatusLabel.attributedText = NSMutableAttributedString("订单状态：") { $0
            .foregroundColor(ColorDef.gray184)
        }.addText(status) { $0
            .foregroundColor(.green)
        }
        
        if model.status == 1 {
            payTimeLabel.text = ""
        } else {
            payTimeLabel.attributedText = NSMutableAttributedString("支付时间：") { $0
                .foregroundColor(ColorDef.gray184)
            }.addText(model.payTime) { $0
                .foregroundColor(.lightBlack51DarkLight230)
            }
        }
        
        if model.status == 1 {
            overdueTimeLabel.text = ""
        } else {
            overdueTimeLabel.attributedText = NSMutableAttributedString("到期时间：") { $0
                .foregroundColor(ColorDef.gray184)
            }.addText(model.endTime) { $0
                .foregroundColor(.lightBlack51DarkLight230)
            }
        }
        
        goPayBtn.isHidden = model.status != 1
    }
    
}
